package com.mezz.bones.framework.base.tree;

import lombok.Data;
import java.util.Arrays;
import java.util.List;

public class TreeBuilderDemo {

    @Data
    public static class Department {
        private String id;
        private String parentId;
        private String name;
        private String description;

        public Department(String id, String parentId, String name, String description) {
            this.id = id;
            this.parentId = parentId;
            this.name = name;
            this.description = description;
        }
    }

    public static void main(String[] args) {
        // 准备测试数据
        List<Department> departments = Arrays.asList(
                new Department("1", "0", "总公司", "总部"),
                new Department("2", "1", "技术部", "研发中心"),
                new Department("3", "1", "市场部", "市场营销中心"),
                new Department("4", "2", "后端组", "后端开发"),
                new Department("5", "2", "前端组", "前端开发"),
                new Department("6", "3", "销售组", "销售团队"),
                new Department("7", "3", "市场组", "市场推广")
        );

        // 创建TreeBuilder实例
        TreeBuilder<Department, String> treeBuilder = new TreeBuilder<>();

        // 构建树结构
        List<TreeNode<String>> tree = treeBuilder.buildTree(
                departments,
                Department::getId,           // ID获取器
                Department::getParentId,     // 父ID获取器
                Department::getName,         // 名称获取器
                Department::getDescription   // 附加信息获取器
        );

        // 打印树结构
        System.out.println("\n全部节点");
        printTree(tree, 0);

        // 测试搜索功能
        System.out.println("\n搜索包含'后端'的节点：");
        treeBuilder.filterTree(tree, "后端");
        printTree(tree, 0);
    }
    /**
     * 打印树结构
     * @param nodes 节点列表
     * @param level 当前层级
     */
    private static void printTree(List<TreeNode<String>> nodes, int level) {
        for (TreeNode<String> node : nodes) {
            // 打印缩进
            for (int i = 0; i < level; i++) {
                System.out.print("    ");
            }
            // 打印节点信息
            System.out.println("├── " + node.getName() +
                    " [ID:" + node.getId() +
                    ", Level:" + node.getLevel() +
                    ", ParentIds:" + node.getParentIds() +
                    "] (" + node.getInfo() + ")");

            // 递归打印子节点
            if (!node.getChildren().isEmpty()) {
                printTree(node.getChildren(), level + 1);
            }
        }
    }
}